<%@ page language="java" import="java.util.*,com.pojo.User" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'itemlist.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<link rel="stylesheet" type="text/css" href="styles.css">
	<link rel="stylesheet" type="text/css" href="jsp/css/styles.css">
  </head>
  
  <body>
  	<div>用户名：<strong>${user.realname }</strong></div>
  		<form id="itemsForm" method="post" action="">
  	<table>
  		<thead>
  			<tr>
  				<td colspan="3">
  				</td>
  				<td colspan="2">
  					<input onclick="editItemList()" type="button" value="批量修改"/>
  				</td>
  				<td colspan="1">
  					<input onclick="deleteItems()" type="button" value="批量删除"/>
  				</td>
  			</tr>
  			<tr>
  				<th>选择</th>
  				<th>商品名</th>
  				<th>价格</th>
  				<th>时间</th>
  				<th>详情</th>
  				<th>操作</th>
  			</tr>
  		</thead>
  		<tbody>
  			<c:forEach items="${itemList}" var="item">
  				<tr>
  					<td> <input type="checkbox" name="items_id" value="${item.id}"> </td>
  					<td>${item.name}</td>
  					<td>${item.price}</td>
  					<td><fmt:formatDate value="${item.createtime}" pattern="yy:MM:dd"/></td>
  					<td>${item.detail}</td>
  					<td><a href="ITEM/EDIT/${item.id }"><input type="button" value="修改"></a></td>
  				</tr>
  			</c:forEach>
  		</tbody>
  	</table>
  		</form>
		<div class="pagnation">
			<a rel="prev"
				href="ITEM/LIST?offset=${param.offset-1>=0?param.offset-1:0}&limit=${param.limit}"><span
				class="paging paging-bound">上一页</span>
			</a>
			<%
				final int length =6;
				int totalItems = (Integer) request.getAttribute("totalItems");
				//int totalItems=100;
				int currentPage = Integer.parseInt(request.getParameter("offset")) + 1;
				int limit = Integer.parseInt(request.getParameter("limit"));
				int headIndex = currentPage > length / 2 ? currentPage - length / 2
						: 1;
				int tailIndex = currentPage + length / 2 < totalItems / limit ? headIndex
						+ length - 1
						: totalItems / limit;
				for (int i = headIndex; i <= tailIndex; ++i) {
			%>
			<a href="ITEM/LIST?limit=10&offset=<%=i-1%>"><span
				class="paging<%=i == currentPage ? " paging-active" : ""%>"><%=i%></span>
			</a>
			<%
				}
			%>
			<a rel="next"
				href="ITEM/LIST?offset=${(param.offset+1)*param.limit<totalItems?param.offset+1:param.offset}&limit=${param.limit}"><span
				class="paging paging-bound">下一页</span>
			</a>
		</div>
	</body>
	<script type="text/javascript">
		function deleteItems(){
			itemsForm.action="ITEM/LIST/DELETE/ALL";
			itemsForm.submit();
		}
		function editItemList(){
			itemsForm.action="ITEM/EDIT/ALL";
			itemsForm.method="post";
			itemsForm.submit();
		}
	</script>
</html>
